package MyQueue;

import MyStack.EmptyStackException;

//先进先出
public class MyQueue implements Operations{
    public Node headNode;

    public int size;

    @Override
    public void offer(int data) {
        Node node = new Node(data);

        if (this.size == 0) {
            this.headNode = node;
            this.size++;
            return;
        }

        Node curNode = this.headNode;

        while (curNode.nextNode != null) {
            curNode = curNode.nextNode;
        }

        node.prevNode = curNode;
        curNode.nextNode = node;
        this.size++;
    }

    @Override
    public int poll() {
        if (this.isEmpty()) {
            throw new EmptyStackException("空队列，无法取出");
        }

        int val = 0;
        if (this.size == 1) {
            val = this.headNode.val;
            this.headNode = null;
        } else {
            Node next = this.headNode.nextNode;
            val = this.headNode.val;

            this.headNode = null;
            this.headNode = next;
            this.headNode.prevNode = null;
            this.size--;
        }

        return val;
    }

    @Override
    public int peek() {
        return this.headNode.val;
    }

    @Override
    public int size() {
        return this.size;
    }

    @Override
    public boolean isEmpty() {
        return this.size == 0;
    }

    public void show() {
        Node curNode = this.headNode;

        while (curNode != null) {
            System.out.println(curNode.val);
            curNode = curNode.nextNode;
        }
        System.out.println();
    }
}
